<template>
  <div class="myself">
    <div class="bg" :style="`backgroundImage:url(${myselfData.userBg})`">
      <van-uploader :after-read="afterRead" />
    </div>
    <div class="info-box">
      <div class="info-top">
        <div class="head-img">
          <img :src="myselfData.userImg" class="auto-img" />
        </div>
        <div class="describe-box">
          <div class="name">{{ myselfData.nickName }}</div>
          <div class="explain">{{ myselfData.desc | descData }}</div>
        </div>
      </div>
      <div class="set-box">
        <div @click="goHomepage">
          <span>个人资料</span>
          <van-icon name="arrow" size="16px" />
        </div>
        <div>
          <span @click="goMyorder">我的订单</span>
          <van-icon name="arrow" size="16px" />
        </div>
        <div @click="goFavorite">
          <span>我的收藏</span>
          <van-icon name="arrow" size="16px" />
        </div>
        <div @click="goAddressList">
          <span>地址管理</span>
          <van-icon name="arrow" size="16px" />
        </div>
        <div @click="goSafety">
          <span>安全中心</span>
          <van-icon name="arrow" size="16px" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import "../assets/less/myself.less";
export default {
  name: "Myself",
  data() {
    return {
      //个人信息
      myselfData: [],
    };
  },
  filters: {
    descData(val) {
      if (val) {
        return val;
      } else {
        return "这个家伙很懒，什么也没有留下";
      }
    },
  },
  created() {
    this.inquireMyself();
  },
  methods: {
    // 跳转到地址管理页面
    goAddressList() {
      this.$router.push({ name: "AddressList" });
    },

    // 跳转到收藏
    goFavorite() {
      this.$router.push({ name: "Favorite" });
    },

    // 跳转个人主页
    goHomepage() {
      this.$router.push({ name: "Homepage" });
    },

    // 跳转到安全中心
    goSafety() {
      this.$router.push({ name: "Safety" });
    },

    // 跳转到我的订单
    goMyorder() {
      this.$router.push({ name: "Myorder" });
    },

    // 查询我的数据
    inquireMyself() {
      let tokenString = this.$cookies.get("tokenString");

      if (!tokenString) {
        return this.$router.push({ name: "Login" });
      }

      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        duration: 0,
      });

      this.axios({
        method: "get",
        url: this.baseUrl + "/findMy",
        params: {
          appkey: this.appkey,
          tokenString: tokenString,
        },
      })
        .then((result) => {
          console.log(result);
          this.$toast.clear();
          if (result.data.code == 700) {
            return this.$router.push({ name: "Login" });
          }
          if (result.data.code === "A001") {
            this.myselfData = result.data.result[0];
            console.log(this.myselfData);
          } else {
            this.$toast({
              message: result.data.msg,
              forbidClick: true,
              duration: 3000,
            });
          }
        })
        .catch((err) => {
          this.$toast.clear();
          console.log(err);
        });
    },
    
    // 修改背景
    afterRead(file) {
      if (!file) {
        return;
      }
      console.log(file);
      // console.log(file.file.type)

      let tokenString = this.$cookies.get("tokenString");

      //当用户未登录的时候跳转到登录页面
      if (!tokenString) {
        return this.$router.push({ name: "Login" });
      }

      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        duration: 0,
      });

      let imgType = file.file.type.split("/")[1];
      console.log(imgType);
      // console.log(file.content.split('base64,'))
      let serverBase64Img = file.content.split("base64,")[1];
      // console.log(imgType,serverBase64Img)

      this.axios({
        method: "post",
        url: this.baseUrl + "/updateUserBg",
        data: {
          appkey: this.appkey,
          tokenString: tokenString,
          imgType: imgType,
          serverBase64Img: serverBase64Img,
        },
      })
        .then((result) => {
          console.log(result);
          this.$toast.clear();
          if (result.data.code === 700) {
            this.$toast({
              message: result.data.msg,
              forbidClick: true,
              duration: 3000,
            });
            return this.$router.push({ name: "Login" });
          }
          if (result.data.code === "I001") {
            this.myselfData.userImg = result.data.result.userImg;
            // this.$router.go(0);
            this.inquireMyself();
          } else {
            this.$toast({
              message: result.data.msg,
              forbidClick: true,
              duration: 3000,
            });
          }
        })
        .catch((err) => {
          this.$toast.clear();
          console.log(err);
        });
    },
  },
};
</script>
